home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / demo / textdrawing / textedit.cpp.z / textedit.cpp
Encoding:
C/C++ Source or Header  |  2002-04-08  |  15.6 KB  |  479 lines

  1. /****************************************************************************
  2. ** $Id:  qt/textedit.cpp   3.0.3   edited Oct 12 12:18 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "textedit.h"
  12.  
  13. #include <qtextedit.h>
  14. #include <qaction.h>
  15. #include <qmenubar.h>
  16. #include <qpopupmenu.h>
  17. #include <qtoolbar.h>
  18. #include <qtabwidget.h>
  19. #include <qapplication.h>
  20. #include <qfontdatabase.h>
  21. #include <qcombobox.h>
  22. #include <qlineedit.h>
  23. #include <qfileinfo.h>
  24. #include <qfile.h>
  25. #include <qfiledialog.h>
  26. #include <qprinter.h>
  27. #include <qpaintdevicemetrics.h>
  28. #include <qsimplerichtext.h>
  29. #include <qcolordialog.h>
  30. #include <qpainter.h>
  31.  
  32. TextEdit::TextEdit( QWidget *parent, const char *name )
  33.     : QMainWindow( parent, name, 0 )
  34. {
  35.     setupFileActions();
  36.     setupEditActions();
  37.     setupTextActions();
  38.  
  39.     tabWidget = new QTabWidget( this );
  40.     connect( tabWidget, SIGNAL( currentChanged( QWidget * ) ),
  41.          this, SLOT( editorChanged( QWidget * ) ) );
  42.     setCentralWidget( tabWidget );
  43. }
  44.  
  45. void TextEdit::setupFileActions()
  46. {
  47.     QToolBar *tb = new QToolBar( this );
  48.     QPopupMenu *menu = new QPopupMenu( this );
  49.     menuBar()->insertItem( tr( "&File" ), menu );
  50.  
  51.     QAction *a;
  52.     a = new QAction( tr( "New" ), QPixmap( "textdrawing/filenew.xpm" ), tr( "&New..." ), CTRL + Key_N, this, "fileNew" );
  53.     connect( a, SIGNAL( activated() ), this, SLOT( fileNew() ) );
  54.     a->addTo( tb );
  55.     a->addTo( menu );
  56.     a = new QAction( tr( "Open" ), QPixmap( "textdrawing/fileopen.xpm" ), tr( "&Open..." ), CTRL + Key_O, this, "fileOpen" );
  57.     connect( a, SIGNAL( activated() ), this, SLOT( fileOpen() ) );
  58.     a->addTo( tb );
  59.     a->addTo( menu );
  60.     menu->insertSeparator();
  61.     a = new QAction( tr( "Save" ), QPixmap( "textdrawing/filesave.xpm" ), tr( "&Save..." ), CTRL + Key_S, this, "fileSave" );
  62.     connect( a, SIGNAL( activated() ), this, SLOT( fileSave() ) );
  63.     a->addTo( tb );
  64.     a->addTo( menu );
  65.     a = new QAction( tr( "Save As" ), QPixmap(), tr( "Save &As..." ), 0, this, "fileSaveAs" );
  66.     connect( a, SIGNAL( activated() ), this, SLOT( fileSaveAs() ) );
  67.     a->addTo( menu );
  68.     menu->insertSeparator();
  69.     a = new QAction( tr( "Print" ), QPixmap( "textdrawing/fileprint.xpm" ), tr( "&Print..." ), CTRL + Key_P, this, "filePrint" );
  70.     connect( a, SIGNAL( activated() ), this, SLOT( filePrint() ) );
  71.     a->addTo( tb );
  72.     a->addTo( menu );
  73.     a = new QAction( tr( "Close" ), QPixmap(), tr( "&Close" ), 0, this, "fileClose" );
  74.     connect( a, SIGNAL( activated() ), this, SLOT( fileClose() ) );
  75.     a->addTo( menu );
  76. }
  77.  
  78. void TextEdit::setupEditActions()
  79. {
  80.     QToolBar *tb = new QToolBar( this );
  81.     QPopupMenu *menu = new QPopupMenu( this );
  82.     menuBar()->insertItem( tr( "&Edit" ), menu );
  83.  
  84.     QAction *a;
  85.     a = new QAction( tr( "Undo" ), QPixmap( "textdrawing/editundo.xpm" ), tr( "&Undo" ), CTRL + Key_Z, this, "editUndo" );
  86.     connect( a, SIGNAL( activated() ), this, SLOT( editUndo() ) );
  87.     a->addTo( tb );
  88.     a->addTo( menu );
  89.     a = new QAction( tr( "Redo" ), QPixmap( "textdrawing/editredo.xpm" ), tr( "&Redo" ), CTRL + Key_Y, this, "editRedo" );
  90.     connect( a, SIGNAL( activated() ), this, SLOT( editRedo() ) );
  91.     a->addTo( tb );
  92.     a->addTo( menu );
  93.     menu->insertSeparator();
  94.     a = new QAction( tr( "Cut" ), QPixmap( "textdrawing/editcut.xpm" ), tr( "&Cut" ), CTRL + Key_X, this, "editCut" );
  95.     connect( a, SIGNAL( activated() ), this, SLOT( editCut() ) );
  96.     a->addTo( tb );
  97.     a->addTo( menu );
  98.     a = new QAction( tr( "Copy" ), QPixmap( "textdrawing/editcopy.xpm" ), tr( "C&opy" ), CTRL + Key_C, this, "editCopy" );
  99.     connect( a, SIGNAL( activated() ), this, SLOT( editUndo() ) );
  100.     a->addTo( tb );
  101.     a->addTo( menu );
  102.     a = new QAction( tr( "Paste" ), QPixmap( "textdrawing/editpaste.xpm" ), tr( "&Paste" ), CTRL + Key_V, this, "editPaste" );
  103.     connect( a, SIGNAL( activated() ), this, SLOT( editPaste() ) );
  104.     a->addTo( tb );
  105.     a->addTo( menu );
  106. }
  107.  
  108. void TextEdit::setupTextActions()
  109. {
  110.     QToolBar *tb = new QToolBar( this );
  111.     QPopupMenu *menu = new QPopupMenu( this );
  112.     menuBar()->insertItem( tr( "For&mat" ), menu );
  113.  
  114.     comboStyle = new QComboBox( FALSE, tb );
  115.     comboStyle->insertItem( tr("Standard") );
  116.     comboStyle->insertItem( tr("Bullet List (Disc)") );
  117.     comboStyle->insertItem( tr("Bullet List (Circle)") );
  118.     comboStyle->insertItem( tr("Bullet List (Square)") );
  119.     comboStyle->insertItem( tr("Ordered List (Decimal)") );
  120.     comboStyle->insertItem( tr("Ordered List (Alpha lower)") );
  121.     comboStyle->insertItem( tr("Ordered List (Alpha upper)") );
  122.     connect( comboStyle, SIGNAL( activated( int ) ),
  123.          this, SLOT( textStyle( int ) ) );
  124.  
  125.     comboFont = new QComboBox( TRUE, tb );
  126.     QFontDatabase db;
  127.     comboFont->insertStringList( db.families() );
  128.     connect( comboFont, SIGNAL( activated( const QString & ) ),
  129.          this, SLOT( textFamily( const QString & ) ) );
  130.     comboFont->lineEdit()->setText( QApplication::font().family() );
  131.  
  132.     comboSize = new QComboBox( TRUE, tb );
  133.     QValueList<int> sizes = db.standardSizes();
  134.     QValueList<int>::Iterator it = sizes.begin();
  135.     for ( ; it != sizes.end(); ++it )
  136.     comboSize->insertItem( QString::number( *it ) );
  137.     connect( comboSize, SIGNAL( activated( const QString & ) ),
  138.          this, SLOT( textSize( const QString & ) ) );
  139.     comboSize->lineEdit()->setText( QString::number( QApplication::font().pointSize() ) );
  140.  
  141.     actionTextBold = new QAction( tr( "Bold" ), QPixmap( "textdrawing/textbold.xpm" ), tr( "&Bold" ), CTRL + Key_B, this, "textBold" );
  142.     connect( actionTextBold, SIGNAL( activated() ), this, SLOT( textBold() ) );
  143.     actionTextBold->addTo( tb );
  144.     actionTextBold->addTo( menu );
  145.     actionTextBold->setToggleAction( TRUE );
  146.     actionTextItalic = new QAction( tr( "Italic" ), QPixmap( "textdrawing/textitalic.xpm" ), tr( "&Italic" ), CTRL + Key_I, this, "textItalic" );
  147.     connect( actionTextItalic, SIGNAL( activated() ), this, SLOT( textItalic() ) );
  148.     actionTextItalic->addTo( tb );
  149.     actionTextItalic->addTo( menu );
  150.     actionTextItalic->setToggleAction( TRUE );
  151.     actionTextUnderline = new QAction( tr( "Underline" ), QPixmap( "textdrawing/textunder.xpm" ), tr( "&Underline" ), CTRL + Key_U, this, "textUnderline" );
  152.     connect( actionTextUnderline, SIGNAL( activated() ), this, SLOT( textUnderline() ) );
  153.     actionTextUnderline->addTo( tb );
  154.     actionTextUnderline->addTo( menu );
  155.     actionTextUnderline->setToggleAction( TRUE );
  156.     menu->insertSeparator();
  157.  
  158.     QActionGroup *grp = new QActionGroup( this );
  159.     grp->setExclusive( TRUE );
  160.     connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) );
  161.  
  162.     actionAlignLeft = new QAction( tr( "Left" ), QPixmap( "textdrawing/textleft.xpm" ), tr( "&Left" ), CTRL + Key_L, grp, "textLeft" );
  163.     actionAlignLeft->addTo( tb );
  164.     actionAlignLeft->addTo( menu );
  165.     actionAlignLeft->setToggleAction( TRUE );
  166.     actionAlignCenter = new QAction( tr( "Center" ), QPixmap( "textdrawing/textcenter.xpm" ), tr( "C&enter" ), CTRL + Key_M, grp, "textCenter" );
  167.     actionAlignCenter->addTo( tb );
  168.     actionAlignCenter->addTo( menu );
  169.     actionAlignCenter->setToggleAction( TRUE );
  170.     actionAlignRight = new QAction( tr( "Right" ), QPixmap( "textdrawing/textright.xpm" ), tr( "&Right" ), CTRL + Key_R, grp, "textRight" );
  171.     actionAlignRight->addTo( tb );
  172.     actionAlignRight->addTo( menu );
  173.     actionAlignRight->setToggleAction( TRUE );
  174.     actionAlignJustify = new QAction( tr( "Justify" ), QPixmap( "textdrawing/textjustify.xpm" ), tr( "&Justify" ), CTRL + Key_J, grp, "textjustify" );
  175.     actionAlignJustify->addTo( tb );
  176.     actionAlignJustify->addTo( menu );
  177.     actionAlignJustify->setToggleAction( TRUE );
  178.  
  179.     menu->insertSeparator();
  180.  
  181.     QPixmap pix( 16, 16 );
  182.     pix.fill( black );
  183.     actionTextColor = new QAction( tr( "Color" ), pix, tr( "&Color..." ), 0, this, "textColor" );
  184.     connect( actionTextColor, SIGNAL( activated() ), this, SLOT( textColor() ) );
  185.     actionTextColor->addTo( tb );
  186.     actionTextColor->addTo( menu );
  187. }
  188.  
  189. void TextEdit::load( const QString &f )
  190. {
  191.     if ( !QFile::exists( f ) )
  192.     return;
  193.     QTextEdit *edit = new QTextEdit( tabWidget );
  194.     doConnections( edit );
  195.     tabWidget->addTab( edit, QFileInfo( f ).fileName() );
  196.  
  197.     QFile fl( f );
  198.     fl.open( IO_ReadOnly );
  199.     QByteArray array = fl.readAll();
  200.     array.resize( array.size() +1 );
  201.     array[ (int)array.size() - 1 ] = '\0';
  202.     QString text = ( f.find( "bidi.txt" ) != -1 ? QString::fromUtf8( array.data() ) : QString::fromLatin1( array.data() ) );
  203.     edit->setText( text );
  204.  
  205.     edit->viewport()->setFocus();
  206.     edit->setTextFormat( Qt::RichText );
  207. }
  208.  
  209. QTextEdit *TextEdit::currentEditor() const
  210. {
  211.     if ( tabWidget->currentPage() &&
  212.      tabWidget->currentPage()->inherits( "QTextEdit" ) )
  213.     return (QTextEdit*)tabWidget->currentPage();
  214.     return 0;
  215. }
  216.  
  217. void TextEdit::doConnections( QTextEdit *e )
  218. {
  219.     connect( e, SIGNAL( currentFontChanged( const QFont & ) ),
  220.          this, SLOT( fontChanged( const QFont & ) ) );
  221.     connect( e, SIGNAL( currentColorChanged( const QColor & ) ),
  222.          this, SLOT( colorChanged( const QColor & ) ) );
  223.     connect( e, SIGNAL( currentAlignmentChanged( int ) ),
  224.          this, SLOT( alignmentChanged( int ) ) );
  225. }
  226.  
  227. void TextEdit::fileNew()
  228. {
  229.     QTextEdit *edit = new QTextEdit( tabWidget );
  230.     doConnections( edit );
  231.     tabWidget->addTab( edit, tr( "noname" ) );
  232.     tabWidget->showPage( edit );
  233.     edit->viewport()->setFocus();
  234. }
  235.  
  236. void TextEdit::fileOpen()
  237. {
  238.     QString fn = QFileDialog::getOpenFileName( QString::null, tr( "HTML-Files (*.htm *.html);;All Files (*)" ), this );
  239.     if ( !fn.isEmpty() )
  240.     load( fn );
  241. }
  242.  
  243. void TextEdit::fileSave()
  244. {
  245.     if ( !currentEditor() )
  246.     return;
  247.     QString fn;
  248.     if ( filenames.find( currentEditor() ) == filenames.end() ) {
  249.     fileSaveAs();
  250.     } else {
  251.     QFile file( *filenames.find( currentEditor() ) );
  252.     if ( !file.open( IO_WriteOnly ) )
  253.         return;
  254.     QTextStream ts( &file );
  255.     ts << currentEditor()->text();
  256.     }
  257. }
  258.  
  259. void TextEdit::fileSaveAs()
  260. {
  261.     if ( !currentEditor() )
  262.     return;
  263.     QString fn = QFileDialog::getSaveFileName( QString::null, tr( "HTML-Files (*.htm *.html);;All Files (*)" ), this );
  264.     if ( !fn.isEmpty() ) {
  265.     filenames.replace( currentEditor(), fn );
  266.     fileSave();
  267.     tabWidget->setTabLabel( currentEditor(), QFileInfo( fn ).fileName() );
  268.     }
  269. }
  270.  
  271. void TextEdit::filePrint()
  272. {
  273.     if ( !currentEditor() )
  274.     return;
  275. #ifndef QT_NO_PRINTER
  276.     QPrinter printer;
  277.     printer.setFullPage(TRUE);
  278.     QPaintDeviceMetrics screen( this );
  279.     printer.setResolution( screen.logicalDpiY() );
  280.     if ( printer.setup( this ) ) {
  281.     QPainter p( &printer );
  282.     QPaintDeviceMetrics metrics( p.device() );
  283.     int dpix = metrics.logicalDpiX();
  284.     int dpiy = metrics.logicalDpiY();
  285.     const int margin = 72; // pt
  286.     QRect body( margin * dpix / 72, margin * dpiy / 72,
  287.             metrics.width() - margin * dpix / 72 * 2,
  288.             metrics.height() - margin * dpiy / 72 * 2 );
  289.     QFont font( "times", 10 );
  290.     QSimpleRichText richText( currentEditor()->text(), font, currentEditor()->context(), currentEditor()->styleSheet(),
  291.                   currentEditor()->mimeSourceFactory(), body.height() );
  292.     richText.setWidth( &p, body.width() );
  293.     QRect view( body );
  294.     int page = 1;
  295.     do {
  296.         richText.draw( &p, body.left(), body.top(), view, colorGroup() );
  297.         view.moveBy( 0, body.height() );
  298.         p.translate( 0 , -body.height() );
  299.         p.setFont( font );
  300.         p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ),
  301.             view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) );
  302.         if ( view.top()  >= richText.height() )
  303.         break;
  304.         printer.newPage();
  305.         page++;
  306.     } while (TRUE);
  307.     }
  308. #endif
  309. }
  310.  
  311. void TextEdit::fileClose()
  312. {
  313.     delete currentEditor();
  314.     if ( currentEditor() )
  315.     currentEditor()->viewport()->setFocus();
  316. }
  317.  
  318. void TextEdit::fileExit()
  319. {
  320.     qApp->quit();
  321. }
  322.  
  323. void TextEdit::editUndo()
  324. {
  325.     if ( !currentEditor() )
  326.     return;
  327.     currentEditor()->undo();
  328. }
  329.  
  330. void TextEdit::editRedo()
  331. {
  332.     if ( !currentEditor() )
  333.     return;
  334.     currentEditor()->redo();
  335. }
  336.  
  337. void TextEdit::editCut()
  338. {
  339.     if ( !currentEditor() )
  340.     return;
  341.     currentEditor()->cut();
  342. }
  343.  
  344. void TextEdit::editCopy()
  345. {
  346.     if ( !currentEditor() )
  347.     return;
  348.     currentEditor()->copy();
  349. }
  350.  
  351. void TextEdit::editPaste()
  352. {
  353.     if ( !currentEditor() )
  354.     return;
  355.     currentEditor()->paste();
  356. }
  357.  
  358. void TextEdit::textBold()
  359. {
  360.     if ( !currentEditor() )
  361.     return;
  362.     currentEditor()->setBold( actionTextBold->isOn() );
  363. }
  364.  
  365. void TextEdit::textUnderline()
  366. {
  367.     if ( !currentEditor() )
  368.     return;
  369.     currentEditor()->setUnderline( actionTextUnderline->isOn() );
  370. }
  371.  
  372. void TextEdit::textItalic()
  373. {
  374.     if ( !currentEditor() )
  375.     return;
  376.     currentEditor()->setItalic( actionTextItalic->isOn() );
  377. }
  378.  
  379. void TextEdit::textFamily( const QString &f )
  380. {
  381.     if ( !currentEditor() )
  382.     return;
  383.     currentEditor()->setFamily( f );
  384.     currentEditor()->viewport()->setFocus();
  385. }
  386.  
  387. void TextEdit::textSize( const QString &p )
  388. {
  389.     if ( !currentEditor() )
  390.     return;
  391.     currentEditor()->setPointSize( p.toInt() );
  392.     currentEditor()->viewport()->setFocus();
  393. }
  394.  
  395. void TextEdit::textStyle( int i )
  396. {
  397.     if ( !currentEditor() )
  398.     return;
  399.     if ( i == 0 )
  400.     currentEditor()->setParagType( QStyleSheetItem::DisplayBlock, QStyleSheetItem::ListDisc );
  401.     else if ( i == 1 )
  402.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDisc );
  403.     else if ( i == 2 )
  404.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListCircle );
  405.     else if ( i == 3 )
  406.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListSquare );
  407.     else if ( i == 4 )
  408.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDecimal );
  409.     else if ( i == 5 )
  410.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListLowerAlpha );
  411.     else if ( i == 6 )
  412.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListUpperAlpha );
  413.     currentEditor()->viewport()->setFocus();
  414. }
  415.  
  416. void TextEdit::textColor()
  417. {
  418.     if ( !currentEditor() )
  419.     return;
  420.     QColor col = QColorDialog::getColor( currentEditor()->color(), this );
  421.     if ( !col.isValid() )
  422.     return;
  423.     currentEditor()->setColor( col );
  424.     QPixmap pix( 16, 16 );
  425.     pix.fill( black );
  426.     actionTextColor->setIconSet( pix );
  427. }
  428.  
  429. void TextEdit::textAlign( QAction *a )
  430. {
  431.     if ( !currentEditor() )
  432.     return;
  433.     if ( a == actionAlignLeft )
  434.     currentEditor()->setAlignment( AlignLeft );
  435.     else if ( a == actionAlignCenter )
  436.     currentEditor()->setAlignment( AlignHCenter );
  437.     else if ( a == actionAlignRight )
  438.     currentEditor()->setAlignment( AlignRight );
  439.     else if ( a == actionAlignJustify )
  440.     currentEditor()->setAlignment( AlignJustify );
  441. }
  442.  
  443. void TextEdit::fontChanged( const QFont &f )
  444. {
  445.     comboFont->lineEdit()->setText( f.family() );
  446.     comboSize->lineEdit()->setText( QString::number( f.pointSize() ) );
  447.     actionTextBold->setOn( f.bold() );
  448.     actionTextItalic->setOn( f.italic() );
  449.     actionTextUnderline->setOn( f.underline() );
  450. }
  451.  
  452. void TextEdit::colorChanged( const QColor &c )
  453. {
  454.     QPixmap pix( 16, 16 );
  455.     pix.fill( c );
  456.     actionTextColor->setIconSet( pix );
  457. }
  458.  
  459. void TextEdit::alignmentChanged( int a )
  460. {
  461.     if ( ( a == AlignAuto ) || ( a & AlignLeft ))
  462.     actionAlignLeft->setOn( TRUE );
  463.     else if ( ( a & AlignHCenter ) )
  464.     actionAlignCenter->setOn( TRUE );
  465.     else if ( ( a & AlignRight ) )
  466.     actionAlignRight->setOn( TRUE );
  467.     else if ( ( a & AlignJustify ) )
  468.     actionAlignJustify->setOn( TRUE );
  469. }
  470.  
  471. void TextEdit::editorChanged( QWidget * )
  472. {
  473.     if ( !currentEditor() )
  474.     return;
  475.     fontChanged( currentEditor()->font() );
  476.     colorChanged( currentEditor()->color() );
  477.     alignmentChanged( currentEditor()->alignment() );
  478. }
  479.